<?php
/*
 * 行情加载的公共函数文件
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: b7927f37f21f6f9b66d80fc1be6ab208d038bf99 $
 */
defined('IN_IXDCW') or exit('Access Denied');
function get_avg($lists) {
	$n = $s = 0;
	foreach($lists as $v) {
		if($v['price'] > 0) {
			$n++;
			$s += $v['price'];
		}
	}
	return $n ? dround($s/$n) : 0;
}

function get_all($date, $lists) {
	$n = $s = 0;
	foreach($lists as $v) {
		if($v['date'] == $date) {
			$n++;
			$s += $v['price'];
		}
	}
	return $n ? dround($s/$n) : get_avg($lists);
}

function get_mkt($date, $market, $lists) {
	$n = $s = 0;
	foreach($lists as $v) {
		if($v['date'] == $date && $v['market'] == $market) {
			$n++;
			$s += $v['price'];
		}
	}
	return $n ? dround($s/$n) : get_avg($lists);
}

function matchCTP($ctp, $cartypeid){
    $tmpCTPS = PCache::read('autoCategory-level.php');
    $match = array(
        '1' => 433,
        '2' => 434,
        '3' => 435,
    );
    $topParentId = $match[$cartypeid];
    $CTP = $tmpCTPS[$topParentId];
    $strChild = $CTP['arrchildid'];
    $arrChild = separate(',', $strChild);
    return in_array($ctp, $arrChild);
}

/**
 * 分割separte, 去除空格
 * @param        $str
 * @param string $separator
 * @return array
 */
function separate($separator, $str) {
    $separator = trim($separator);
    if (strpos($str, $separator) !== false) {
        $arrStr = explode($separator, $str);
        $return = array_map('trim', $arrStr);
    } else {
        $return = array($str);
    }
    return $return;
}